/**
 * @Title: StockController.java
 * @Description: TODO(用一句话描述该文件做什么)
 * @author 张鑫
 * @phone 17561783855
 * @date 2019年4月16日
 */
package com.fortune.rainshine.service.controller;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fortune.rainshine.common.BaseController;
import com.fortune.rainshine.common.utils.MyMD5Util;
import com.fortune.rainshine.common.utils.StringUtil;
import com.fortune.rainshine.service.service.StockService;
import com.fortune.rainshinecommon.base.MsgModel;
import com.fortune.rainshinecommon.base.ParmsModel;
import com.fortune.rainshinecommon.enums.ResultCode;
import com.fortune.rainshinecommon.model.service.StockModel;

/**
 * @ClassName: StockController
 * @Description: TODO(库存操作)
 * @author 张鑫
 * @phone 17561783855
 * @date 2019年4月16日
 *
 */
@Controller
@RequestMapping("/service/stock")
public class StockController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(StockController.class);

    @Autowired
    private StockService stockService;

    @RequiresPermissions("stock:view")
    @RequestMapping()
    public String stock(Model model, HttpServletRequest request) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        //获取当前商铺id
    	String parm = request.getHeader("Pass-Check-Session");
    	String parm2 = request.getHeader("Pass-Check-Session").substring(16, parm.length());
    	String shopId = parm2.substring(0,parm2.length()-9);
        //根据店铺id获取该店库存详情
        Object stocks = stockService.getStocksByShopId(shopId).getData();
        model.addAttribute("stocks", stocks);
        //获取用户权限
        Set<String> authorities = getLoginUserAuthorities();
        //获取登录用户的按钮展示情况
        model.addAttribute("warning", authorities.contains("stock:warning"));
        model.addAttribute("stockRecord", authorities.contains("stock:stockRecord"));
        model.addAttribute("addStock", authorities.contains("stock:purchase"));
        model.addAttribute("reduceStock", authorities.contains("stock:purchase"));
        return "stock/stock.html";
    }

    @RequiresPermissions("stock:warning")
    @ResponseBody
    @RequestMapping("/warning")
    public MsgModel warning(@RequestBody StockModel model, HttpServletRequest request) {
        model.setUser(this.getLoginUser());
        //获取当前商铺id
        String parm = request.getHeader("Pass-Check-Session");
    	String parm2 = request.getHeader("Pass-Check-Session").substring(16, parm.length());
    	Integer shopId = Integer.valueOf(parm2.substring(0,parm2.length()-9));
        model.setShopId(shopId);
        if (stockService.warning(model)) {
            return new MsgModel("修改报警数量成功", ResultCode.SUCCESS);
        } else {
            return new MsgModel("修改报警数量失败", ResultCode.FAILURE);
        }

    }

    @RequiresPermissions("stock:purchase")
    @ResponseBody
    @RequestMapping("/purchase")
    public MsgModel purchase(@RequestBody StockModel model, HttpServletRequest request) {
    	 //获取当前商铺id
        String parm = request.getHeader("Pass-Check-Session");
    	String parm2 = request.getHeader("Pass-Check-Session").substring(16, parm.length());
    	Integer shopId = Integer.valueOf(parm2.substring(0,parm2.length()-9));
        model.setShopId(shopId);
        model.setCreateId(this.getLoginUser().getUserId());
        return stockService.purchase(model);
    }

    /*
     * @RequiresPermissions("stock:stockRecord")
     *
     * @RequestMapping("/stockRecord") public String detailForm(String id,Model
     * model) { List<StockRecordsModel> records = stockService.findDetailStocks(id);
     * model.addAttribute("records",new Gson().toJson(records)); return
     * "stock/stockDetail.html"; }
     */
    @RequiresPermissions("stock:stockRecord")
    @RequestMapping("/stockRecord")
    @ResponseBody
    public MsgModel stockRecordDetail(Integer page, Integer limit, String searchKey, String searchValue, Integer type) {
        try {
            if (page == null) {
                page = 0;
                limit = 0;
            }
            if (StringUtil.isBlank(searchValue)) {
                searchKey = null;
            }
            ParmsModel parm = new ParmsModel(page, limit, searchKey, searchValue);
            parm.setType(type);
            return stockService.stockRecordDetail(parm);
        } catch (Exception e) {
            logger.error("web stockRecordDetail ====> {}", e);
            return new MsgModel(ResultCode.FAILURE);
        }
    }

}
